package data

import (
	protoV1 "Cosmos.Communication/api/v1"
	protoV2 "Cosmos.Communication/api/v2"
	"Cosmos.Communication/infrastructure/grpc"
)

type grpcClientFactory struct {
	pool    *grpc.GrpcConnPool
	options *grpc.GrpcConnOptions
}

func NewGrpcClientFactory(options *grpc.GrpcConnOptions) (g *grpcClientFactory, err error) {
	pool, err := grpc.NewGrpcConnPool(options)
	if err != nil {
		return
	}

	grpc.RealAddr = options.Addr
	g = &grpcClientFactory{
		pool:    pool,
		options: options,
	}
	return
}

func (g *grpcClientFactory) GetDataSetGrpcClient() (client protoV2.RpcDataSetServiceClient, err error) {
	conn, err := g.pool.GetConn()
	if err != nil {
		return
	}

	/*var conn *ggRpc.ClientConn
	if conn, err = grpc.Dial(g.options.Addr); err != nil {
		return
	}*/
	client = protoV2.NewRpcDataSetServiceClient(conn)
	return
}

func (g *grpcClientFactory) GetVendorRuntimeGrpcClient() (client protoV1.VendorRuntimeServiceClient, err error) {
	conn, err := g.pool.GetConn()
	if err != nil {
		return
	}

	/*var conn *ggRpc.ClientConn
	if conn, err = grpc.Dial(g.options.Addr); err != nil {
		return
	}*/
	client = protoV1.NewVendorRuntimeServiceClient(conn)
	return
}

func (g *grpcClientFactory) Close() {
	//g.pool.Close()
}
